package servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import connection.DbConnection;

import bean.Poll;
import bean.Question;

import model.CreateMQuestionImpl;
public class AddMQuestionServlet extends HttpServlet {

	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 4560542739398486993L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost (request, response);
	}
	
	public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("GBK ");
		String options = request.getParameter("optionslist");
		String optionlist[] = {""};
		if(options != null){
			System.out.println("OPTIONS:"+options);
			optionlist = options.split(",");			
		}else{
			System.out.println("NO OPTIONS");
		}
		String text = request.getParameter("text");
		String template = request.getParameter("TemplateQuestion");
		String font = request.getParameter("font");
		String size = request.getParameter("size");
		String color = request.getParameter("color");
		String chartstyle= request.getParameter("chartstyle");
		String timer= request.getParameter("timer");
		String RC= request.getParameter("RC");
		String CAI= request.getParameter("CAI");
		
		try {
			String sql;
			DbConnection db = new DbConnection();
			Connection conn = db.getConn();
			sql = "insert into Question (text, status, TemplateQuestion, font, fontsize, color, timer, rescounter, cai) values (?,?,?,?,?,?,?,?,?)";
			PreparedStatement st = conn.prepareStatement(sql);
			st.setString(1, text);
			st.setString(2, "0");
			st.setString(3, template);
			st.setString(4, font);
			st.setString(5, size);
			st.setString(6, color);
			st.setString(7, timer);
			st.setString(8, RC);
			st.setString(9, CAI);
			st.executeQuery();
			db.close();

			//also insert into adjacent table user_question
			String id = request.getSession().getAttribute("pollid").toString();
			String username = request.getSession().getAttribute("username").toString();
			DbConnection db3 = new DbConnection();
			Connection conn3 = db.getConn();
			sql = "INSERT INTO User_Question VALUES('"+username+"', (SELECT MAX(Question.questionid) FROM Question))";
			PreparedStatement st3 = conn3.prepareStatement(sql);
			st3.executeQuery(sql);
			db3.close();
			
			//and into poll_question
			DbConnection db2 = new DbConnection();
			Connection conn2 = db.getConn();
			sql = "INSERT INTO Poll_Question(PollID, PollVersion, QuestionID, PollName) VALUES("+id+", (SELECT MAX(version) FROM Poll WHERE ID="+id+"), "+
				"(SELECT MAX(Question.questionid) FROM Question), '" 
				+request.getSession().getAttribute("pollName")+"')";
			PreparedStatement st2 = conn2.prepareStatement(sql);
			st2.executeQuery(sql);
			db2.close();

			
			//insert options! :D 
			DbConnection db4 = new DbConnection();
			Connection conn4 = db.getConn();
			int optionid = 0;
			for(String s : optionlist){
				String option[] = s.split("-");
				String optext = option[0];
				String opcorrect = option[1];
				String oppoints = option[2];
				if(opcorrect.equals("true")){
					opcorrect = "y";
				}else{
					opcorrect = "n";
				}
				sql = "INSERT INTO Options(QuestionID,OptionID, Text,CorrectChoice,Points) VALUES("+
				"(SELECT MAX(Question.questionid) FROM Question WHERE text = '"+text+"'),"+optionid+",'"+optext+"','"+opcorrect+"',"+oppoints+")";
				conn4.prepareStatement(sql).executeQuery();
				optionid++;
			}
			db4.close();
			String forward= "Addquestion.jsp";
			response.sendRedirect(forward);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
